/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.own.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jeesite.common.codec.EncodeUtils;
import com.jeesite.common.mapper.JsonMapper;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.own.entity.OwnMedicineReceiptInformation;
import com.jeesite.modules.own.service.OwnMedicineReceiptInformationService;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * 药材收获信息Controller
 * @author F
 * @version 2019-09-27
 */
@Controller
@RequestMapping(value = "${adminPath}/own/ownMedicineReceiptInformation")
public class OwnMedicineReceiptInformationController extends BaseController {

	@Autowired
	private OwnMedicineReceiptInformationService ownMedicineReceiptInformationService;
	
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public OwnMedicineReceiptInformation get(String id, boolean isNewRecord) {
		return ownMedicineReceiptInformationService.get(id, isNewRecord);
	}

	/**
	 * 根据ID查询数据
	 */
	@RequiresPermissions("own:ownMedicineReceiptInformation:view")
	@RequestMapping(value = "findByModelId")
	public OwnMedicineReceiptInformation findByModelId(OwnMedicineReceiptInformation ownMedicineReceiptInformation) {
		return ownMedicineReceiptInformationService.get(ownMedicineReceiptInformation);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("own:ownMedicineReceiptInformation:view")
	@RequestMapping(value = {"list", ""})
	public String list(OwnMedicineReceiptInformation ownMedicineReceiptInformation, Model model) {
		model.addAttribute("ownMedicineReceiptInformation", ownMedicineReceiptInformation);
		return "modules/own/ownMedicineReceiptInformationList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("own:ownMedicineReceiptInformation:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<OwnMedicineReceiptInformation> listData(OwnMedicineReceiptInformation ownMedicineReceiptInformation, HttpServletRequest request, HttpServletResponse response) {
		ownMedicineReceiptInformation.setPage(new Page<>(request, response));
		Page<OwnMedicineReceiptInformation> page = ownMedicineReceiptInformationService.findPage(ownMedicineReceiptInformation);
		return page;
	}

	/**
	 * 查询未检验列表数据
	 */
	@RequiresPermissions("own:ownMedicineReceiptInformation:view")
	@RequestMapping(value = "listDataNormal")
	@ResponseBody
	public Page<OwnMedicineReceiptInformation> listDataNormal(OwnMedicineReceiptInformation ownMedicineReceiptInformation, HttpServletRequest request, HttpServletResponse response) {
		ownMedicineReceiptInformation.setPage(new Page<>(request, response));
		ownMedicineReceiptInformation.setInspectionSituation("未检验");
		Page<OwnMedicineReceiptInformation> page = ownMedicineReceiptInformationService.findPage(ownMedicineReceiptInformation);
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("own:ownMedicineReceiptInformation:view")
	@RequestMapping(value = "form")
	public String form(OwnMedicineReceiptInformation ownMedicineReceiptInformation, Model model) {
		model.addAttribute("ownMedicineReceiptInformation", ownMedicineReceiptInformation);
		return "modules/own/ownMedicineReceiptInformationForm";
	}

	/**
	 * 保存药材收获信息
	 */
	@RequiresPermissions("own:ownMedicineReceiptInformation:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated OwnMedicineReceiptInformation ownMedicineReceiptInformation) {
		String receiptBmBatchCode = UUID.randomUUID().toString();
		ownMedicineReceiptInformation.setBmBatchCode(receiptBmBatchCode);
		ownMedicineReceiptInformationService.save(ownMedicineReceiptInformation);
		return renderResult(Global.TRUE, text("保存药材收获信息成功！"));
	}
	
	/**
	 * 删除药材收获信息
	 */
	@RequiresPermissions("own:ownMedicineReceiptInformation:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(OwnMedicineReceiptInformation ownMedicineReceiptInformation) {
		ownMedicineReceiptInformationService.delete(ownMedicineReceiptInformation);
		return renderResult(Global.TRUE, text("删除药材收获信息成功！"));
	}

	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("own:ownMedicineReceiptInformation:view")
	@RequestMapping(value = "findByKey")
	public String plantInfoSelect(OwnMedicineReceiptInformation ownMedicinePlantInformation, String selectData, Model model) {
		String selectDataJson = EncodeUtils.decodeUrl(selectData);
		if (JsonMapper.fromJson(selectDataJson, Map.class) != null){
			model.addAttribute("selectData", selectDataJson);
		}
		model.addAttribute("receiptInformation", ownMedicinePlantInformation);
		return "modules/own/ownMedicineReceiptInformationSelect";
	}

	/**
	 * 根据id查询数据
	 */
	@RequiresPermissions("own:ownMedicineReceiptInformation:view")
	@RequestMapping(value = "findById")
	public Map<String, Object> findById(String id) {
		Map<String, Object> map = new HashMap<>();
		map.put("success", true);
		map.put("msg", "获取成功");
		map.put("data", null);
		try {
			map.put("data", ownMedicineReceiptInformationService.findById(id));
		}catch (Exception e){
			map.put("success", false);
			map.put("msg", e.getMessage());
		}finally {
			return map;
		}
	}
	
}